/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.mochaccino.framework.jmx.thread;

import com.google.code.mochaccino.framework.jmx.JmxBean;
import com.google.code.mochaccino.framework.jmx.JmxConnection;
import com.google.code.mochaccino.framework.jmx.JmxValue;
import com.google.code.mochaccino.framework.jmx.JmxValueMethod;
import java.util.LinkedList;
import java.util.List;
import javax.management.openmbean.CompositeData;

/** Thread Ids */
public class JmxValueThreads implements JmxValue<JmxValueThread[]> {

	private JmxBean          bean       = null;
	private boolean          cached     = true;
	private JmxConnection    connection = null;
	private String           name       = null;
	private JmxValue<long[]> threadIds  = null;

	private JmxValueThread[] threads = null;

	public JmxBean getBean() {
		return bean;
	}

	public void setBean( JmxBean bean ) {
		this.bean = bean;
	}

	public JmxConnection getConnection() {
		return connection;
	}

	public void setConnection( JmxConnection connection ) {
		this.connection = connection;
	}

	/** @return the name */
	@Override
	public String getName() {
		return name;
	}

	/** @param name the name to set */
	public void setName( String name ) {
		this.name = name;
	}

	/** @return the threadIds */
	public JmxValue<long[]> getThreadIds() {
		return threadIds;
	}

	/** @param threadIds the threadIds to set */
	public void setThreadIds( JmxValue<long[]> threadIds ) {
		this.threadIds = threadIds;
	}

	/* (non-Javadoc)
	 * @see com.eightd.inspector.jmx.JmxValue#getValue()
	 */
	@Override
	public JmxValueThread[] getValue() {
		if ( cached && (threads != null) ) {
			return threads;
		}
		threads = getRawValue();
		return threads;
	}

	private JmxValueThread[] getRawValue() {
		long[] threadIdArray = threadIds.getValue();
		if ( threadIdArray == null ) {
			return new JmxValueThread[0];
		}
		JmxValueMethod<CompositeData[]> method = new JmxValueMethod<CompositeData[]>();
		method.setBean( bean );
		method.setConnection( connection );
		method.setMethodName( "getThreadInfo" );
		method.setSignature( new String[] { long[].class.getName(),
		                                    boolean.class.getName(),
		                                    boolean.class.getName() } );
		method.setParameters( new Object[] { threadIdArray,
		                                     true,
		                                     true } );
		CompositeData[] rawDataArray = method.getValue();
		List<JmxValueThread> threadList = new LinkedList<JmxValueThread>();
		for ( CompositeData rawData : rawDataArray ) {
			if ( rawData == null ) {
				continue;
			}
			threadList.add( new JmxValueThread( rawData ) );
		}
		return threadList.toArray( new JmxValueThread[threadList.size()] );
	}

	public boolean isCached() {
		return cached;
	}

	public void setCached( boolean cached ) {
		this.cached = cached;
	}
}
